//
//  SelectStep2ViewController.m
//  OutesIOS
//
//  Created by norman on 2019/1/12.
//  Copyright © 2019 norman. All rights reserved.
//

#import "SelectStep2ViewController.h"
#import "SelectStep3ViewController.h"
#import "Project_Step1ViewController.h"
#import "Project_Step5ViewController.h"
#import "Project_Step6ViewController.h"
#import "DefaultStyleTableViewCell.h"
#import "BottomActionView.h"
#import "BigMachine_TabViewController.h"
#import "BuildingInfoViewController.h"
#import "BuildingData.h"
#import "ProjectManager.h"
#import "ProjectData.h"
#import <MBProgressHUD.h>
#import "RoomNameViewController.h"
#import "UITapView.h"
#import "SelectXSViewController.h"
#import "IndoorEquipDataResp.h"
#import "TwoLineWithDateCell.h"
#import "BuildingTableViewCell.h"
#import "ProjectContentViewController.h"

@interface SelectStep2ViewController () {
    MBProgressHUD *HUD;
}

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) BottomActionView *footerView;

@end

@implementation SelectStep2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"建筑信息";
    
    //自定义返回按钮
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithTitle:@"项目" style:UIBarButtonItemStyleDone target:self action:@selector(onBackToProject)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    //添加按钮
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"添加建筑" style:UIBarButtonItemStyleDone target:self action:@selector(addNewBuilding)];
    self.navigationItem.rightBarButtonItem = addBtn;
    
    //底部的上一步，下一步按钮
    self.footerView = [[BottomActionView alloc]initWithFrame:CGRectMake(0, self.view.height - KAction_Bar_Height - kNavigation_Bar_Height - kStatus_Bar_Height, kScreen_Width, KAction_Bar_Height)];
    if (iPhone_X) {
        self.footerView.y -= 34;
    }
    __block SelectStep2ViewController *tmp = (SelectStep2ViewController*)self;
    [self.footerView addLeftBtnBlock:^(id obj) {
        //需要跳过选择模板的步骤，所以返回到第一个页面
        //返回到第一个页面 调用2次，跳过2个页面，使得当期步骤是在1
        [tmp.navigationController popViewControllerAnimated:YES];
    }];
    [self.footerView addRightBtnBlock:^(id obj) {
        if (kHeatProject.buildings == nil || kHeatProject.buildings.count == 0) {
            [Tools showMessageCenter:@"项目中至少要有一个建筑!"];
            return;
        }
        BOOL hasEquip = NO;
        for (BuildingData *item in kHeatProject.buildings) {
            if (![Tools isEmpty:item.equip]) {
                hasEquip = YES;
                break;
            }
        }
        if (!hasEquip) {
            [Tools showMessageCenter:@"项目需要选择主机设备!"];
            return;
        }
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        [params setValue:kHeatProject.heat_project_id forKey:@"heat_project_id"];
        
        [MBProgressHUD showMessage:@"项目报告生成中..." toView:tmp.view];
        [HttpClient getWithUrl:HTTP_HEAT_GET_PROJECT_REPORT token:[AccountManager manager].token paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
            [MBProgressHUD hideHUDForView:tmp.view];
            
            SelectStep3ViewController *view = [SelectStep3ViewController new];
            view.report1 = obj.report1;
            view.report2 = obj.report2;
            view.project_name = kHeatProject.heat_project_name;
            [tmp.navigationController pushViewController:view animated:YES];
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:tmp.view];
            [Tools showMessageCenter:@"生成失败！"];
        }];
        
    }];
    [self.footerView setLeftBtnTitle:@"上一步" andRightBtnTitle:@"下一步" andCenterTitle:@"2/3"];
    [self.view addSubview:self.footerView];
    
    //列表
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNavBottom] style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void) generateReport {
    
    
    
    
}

/**
 *  直接跳转到项目页面
 */
-(void) onBackToProject{
    //返回到首页
    for (UIViewController *tmp in self.navigationController.viewControllers) {
        if ([tmp isKindOfClass:[BigMachine_TabViewController class]]) {
            [(BigMachine_TabViewController*)tmp loadData];
            [self.navigationController popToViewController:tmp animated:YES];
        }
    }
}

#pragma 数据处理
/**
 *  添加新建筑
 */
- (void)addNewBuilding {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:kHeatProject.heat_project_id forKey:@"heat_project_id"];
    [HttpClient getWithUrl:HTTP_GET_HEAT_PROJECT_BUILDING_NEW token:[AccountManager manager].token paramDic:params resultClass:[BuildingData class] success:^(BuildingData *obj) {
        if (kHeatProject.buildings == nil) {
            kHeatProject.buildings = [NSMutableArray new];
        }
        [kHeatProject.buildings addObject:obj];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
    }];
}

- (void)deleteBuilding:(NSInteger) tag {
    BuildingData *date = [kHeatProject.buildings objectAtIndex:tag];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:date.building_id forKey:@"building_id"];
    [HttpClient getWithUrl:HTTP_GET_HEAT_PROJECT_BUILDING_DELETE token:[AccountManager manager].token paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        [kHeatProject.buildings removeObjectAtIndex:tag];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [Tools showMessageCenter:@"删除建筑失败！"];
    }];
}


#pragma mark - tableView显示设置数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return kHeatProject.buildings.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"ProjectModuleCell";
    BuildingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[BuildingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BuildingData *item = [kHeatProject.buildings objectAtIndex:indexPath.row];
    [cell setData:item];
    //滑动按钮
    cell.rightUtilityButtons = [self rightButtons];
    cell.delegate = self;
    cell.tag = indexPath.row;
    
    return cell;
}

-(NSArray *)rightButtons {
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor redColor] title:@"删除"];
    return rightUtilityButtons;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BuildingInfoViewController *view = [BuildingInfoViewController new];
    view.currentIndex = indexPath.row;
    view.buildingInfoChangedBlock = ^() {
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:view animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [BuildingTableViewCell cellHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


#pragma mark - 滑动按钮代理协议
/**
 *  右侧滑动按钮点击事件
 *
 *  @param cell  单元cell
 *  @param index 下标
 */
-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index{
    switch (index) {
        case 0:{
            [self deleteBuilding:cell.tag];
            break;
        }
            
        default:
            break;
    }
    [cell hideUtilityButtonsAnimated:YES];
}


/**
 *  滑动的时候把其他的隐藏掉
 *
 *  @param cell cell
 *
 *  @return 隐藏其他的cell
 */
-(BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
